package com.qkl.xiatang.app.service.impl;


import com.qkl.xiatang.app.dto.feed.DecrQtyDTO;
import com.qkl.xiatang.app.entity.feed.FeedStockLogEntity;
import com.qkl.xiatang.app.mapper.FeedAllocMapper;
import com.qkl.xiatang.app.mapper.FeedManagerMapper;
import com.qkl.xiatang.app.mapper.FeedStockLogMapper;
import com.qkl.xiatang.app.utils.AppSessionUtils;
import com.qkl.xiatang.app.utils.AppUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 饲料库存
 */
@Service
@Slf4j
public class FeedStockService {

    @Autowired
    private FeedAllocMapper feedAllocMapper;

    @Autowired
    private FeedStockLogMapper feedStockLogMapper;


    @Autowired
    private AppThreadPoolService appThreadPool;

    @Autowired
    private FeedManagerMapper feedManagerMapper;

    @Autowired
    private MessageService messageService;


    /**
     * 记录饲料库存日志
     * @param companyId
     * @param userName
     * @param isIncr
     * @param decrQtyDTOS
     */
    public void addStockLog(String companyId, String userName, boolean isIncr, List<DecrQtyDTO> decrQtyDTOS){
        appThreadPool.execute(()->{
            addStockLog1(companyId,userName,isIncr,decrQtyDTOS);
        });
    }
    private void addStockLog1(String companyId, String userName, boolean isIncr, List<DecrQtyDTO> decrQtyDTOS){
        log.info("==addStockLog1.companyId={},userName={},decrQtyDTOS={}",companyId,userName,decrQtyDTOS);
        if(ObjectUtils.isEmpty(decrQtyDTOS)){
            return;
        }
        List<FeedStockLogEntity> list = new ArrayList<>();
        List<String> feedIdList = new ArrayList<>();
        Integer flag = isIncr? 1:0;
        for(DecrQtyDTO decrQtyDTO : decrQtyDTOS){
            Double qty = decrQtyDTO.getQty();
            if(null == qty){
                continue;
            }
            if( !isIncr){
                qty = -qty;
            }
            FeedStockLogEntity logEntity = new FeedStockLogEntity();
            logEntity.setId(AppUtils.genIdStr());
            logEntity.setFeedId(decrQtyDTO.getFeedId());
            logEntity.setLogDate(AppUtils.todayYyyyMMdd());
            logEntity.setCreateTime(new Date());
            logEntity.setCompanyId(companyId);
            logEntity.setUserName(userName);
            logEntity.setQty(qty);
            logEntity.setFlag(flag);

            list.add(logEntity);
            feedIdList.add(decrQtyDTO.getFeedId());
        }
        feedStockLogMapper.addFeedStockLog(list);
        messageService.addStockWarnMessage(companyId,feedManagerMapper.queryFeedStockWarnList(feedIdList));

    }


    /**
     * 是否增加库存
     * @param isIncr
     * @param decrQtyDTOS
     */
    public void feedStock(String userId,boolean isIncr,List<DecrQtyDTO> decrQtyDTOS){
        if(ObjectUtils.isNotEmpty(decrQtyDTOS)){
            for (DecrQtyDTO decrQtyDTO: decrQtyDTOS){
                Double qty = decrQtyDTO.getQty();
                if(isIncr && null != qty){
                    qty = -qty;
                }
                feedAllocMapper.decrFeedStock(userId,decrQtyDTO.getFeedId(),qty);
            }
        }
        this.addStockLog(AppSessionUtils.getCompanyId(),AppSessionUtils.getUserName(),isIncr,decrQtyDTOS);
    }

    public void feedStock(String userId,boolean isIncr,DecrQtyDTO decrQtyDTO){
        List<DecrQtyDTO> decrQtyDTOS = new ArrayList<>();
        decrQtyDTOS.add(decrQtyDTO);
        this.feedStock(userId,isIncr,decrQtyDTOS);
    }

    public void feedStock(String userId, boolean isIncr, String feedId , Double qty){
        DecrQtyDTO decrQtyDTO = new DecrQtyDTO();
        decrQtyDTO.setQty(qty);
        decrQtyDTO.setFeedId(feedId);
        this.feedStock(userId,isIncr,decrQtyDTO);
    }


}
